#!/bin/bash
# Copyright (C) 2019 Checkmk GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.

# This extracts all the files of the current commit and passes it to `pre-commit` so the
# hooks can test the commit for validity.
#
# Optionally you can pass "diff" as the first parameter, then it will check the diff between
# the local HEAD and the HEAD of origin/master.
#
# You can use this script to lint your tree in a rebase like so:
#
#      git rebase --exec scripts/check-current-commit
#
# Or if you already have a commit which is ready, just execute
#
#      scripts/check-current-commit
#
REPO_PATH="$(git rev-parse --show-toplevel)"
UVENV=$REPO_PATH/scripts/run-uvenv

if [ "$1" == "diff" ]; then
    HEAD=origin/master..HEAD
else
    HEAD=HEAD
fi

# shellcheck disable=SC2046  # No, don't quote this
${UVENV} pre-commit run --files $(git diff-tree --no-commit-id --name-only -r $HEAD)
